﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Husb.Util;

using CentralOffice.Admin.Data;
using ProcessTable = CentralOffice.Admin.Data.ApprovalProcessDataSet.ApprovalProcessDataTable;
using ProcessRow = CentralOffice.Admin.Data.ApprovalProcessDataSet.ApprovalProcessRow;
using StepTable = CentralOffice.Admin.Data.ApprovalStepDataSet.ApprovalStepDataTable;
using StepRow = CentralOffice.Admin.Data.ApprovalStepDataSet.ApprovalStepRow;
using System.Collections;
using System.Collections.Specialized;

namespace CentralOffice.WebClient.Admin.Flow
{
    public partial class ApprovalProcess : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void gv_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = gv.SelectedRow;

            //object role = row.DataItem;
            //PopulateCategoryTree(row.Cells[1].Text);
            //Message.Text = "You selected " + row.Cells[2].Text + ".";

            Guid processId = (Guid)gv.DataKeys[row.RowIndex].Value;

            StepTable steps = CentralOffice.Admin.BusinessActions.ApprovalStep.GetStepByProcessId(processId);

            //string item = "nothing";
            //if (steps != null && steps.Rows.Count > 0)
            //{
            //    item = processId.ToString();
            //}
            sm.RegisterDataItem(upStep, processId.ToString());//upStep
            gvStep.DataSource = steps;
            gvStep.DataBind();
            upStep.Update();

            
        }

        protected void odsApprovalProcess_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            IDictionaryEnumerator dictionaryEnumerator = e.InputParameters.GetEnumerator();
            while (dictionaryEnumerator.MoveNext())
            {
                if (dictionaryEnumerator.Key.ToString() == "Id")
                {
                    object s = dictionaryEnumerator.Value;
                    break;
                }
            }
            IOrderedDictionary parameters = e.InputParameters;

            Guid processId = (Guid)parameters["Id"];
            StepTable steps = CentralOffice.Admin.BusinessActions.ApprovalStep.GetStepByProcessId(processId);

            if (steps.Rows.Count > 0)
            {
                sm.RegisterDataItem(upProcess, "1");
                e.Cancel = true;
            }
        }

        protected void odsApprovalProcess_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            upProcess.Update();
        }

        protected void gvStep_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Guid stepId = (Guid)gvStep.DataKeys[e.RowIndex].Value;
            CentralOffice.Admin.BusinessActions.ApprovalStep.DeleteById(stepId);
            sm.RegisterDataItem(gvStep, "s");

            GridViewRow row = gv.SelectedRow;

            Guid processId = (Guid)gv.DataKeys[row.RowIndex].Value;
            StepTable steps = CentralOffice.Admin.BusinessActions.ApprovalStep.GetStepByProcessId(processId);

            sm.RegisterDataItem(upStep, processId.ToString());//upStep
            gvStep.DataSource = steps;
            gvStep.DataBind();
            upStep.Update();
        }

        protected void gvStep_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            //
            //sm.RegisterDataItem(upStep, "s");

        }
    }
}
